package com.yunti.springboot.config.log;

import com.alibaba.fastjson.JSON;
import com.yunti.springboot.bo.UserBO;
import com.yunti.springboot.po.ApiLog;
import com.yunti.springboot.util.WebNbUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

public class ApiLogConfig {
    public static final String APILOG_OBJ_SAVE_KEY = "APILOG_OBJ_SAVE_KEY";
    public static final String APILOG_OBJ_SAVE_ID_KEY = "APILOG_OBJ_SAVE_ID_KEY";

    public static void setLog(HttpServletRequest httpServletRequest, String token, UserBO user) {
        ApiLog a = new ApiLog().setReqIp(WebNbUtil.getIP(httpServletRequest)).setReqApi(httpServletRequest.getRequestURI()).setReqParame(JSON.toJSONString(WebNbUtil.getParamsMap2(httpServletRequest))).setReqToken(token).setReqHeader(JSON.toJSONString(WebNbUtil.getHeaderMap(httpServletRequest))).setReqType(httpServletRequest.getMethod()).setStartTime(new Date()).setUserName(user.getPhone()).setUserId(user.getId());
        httpServletRequest.setAttribute(ApiLogConfig.APILOG_OBJ_SAVE_KEY, a);
    }

    public static void setLog(HttpServletRequest httpServletRequest, ApiLog apiLog) {
        httpServletRequest.setAttribute(ApiLogConfig.APILOG_OBJ_SAVE_KEY, apiLog);
    }
}
